/*
信号定义与说明：
*/
module traffic(CLK,EN,LAMPA,LAMPB,ACOUNT,BCOUNT);
input CLK,EN;
output [3:0] LAMPA,LAMPB;
output [7:0] ACOUNT,BCOUNT;
reg tempa,tampb;
reg [2:0] counta,countb;
reg [3:0] LAMPA,LAMPB;
reg [7:0] ared,ayellow,agreen,aleft,brad,byellow,bgreen,bleft;
reg [7:0] numa,numb;

always @(EN)
begin
if(!EN)
	begin
	ared<=8'd55;	ayellow<=8'd5;
	agreen<=8'd40;		aleft<=8'd15;
	bred<=8'd65;	byellow<=8'd5;
	bleft<=8'd15;	bgreen<=8'd30;
	end
assign ACOUNT=numa;
assign BCOUNT=numb;

always @(posedge CLK)
begin
	if(EN)
	begin tempa<=1;
	case(counta)			//亮灯顺序
	0:begin numa<=agreen;	LAMPA<=2;	counta<=1;end
	1:begin numa<=ayellow;  LAMPA<=4;	counta<=2;end
	2:begin numa<=aleft;    LAMPA<=1;	counta<=3;end
	3:begin numa<=ayellow;  LAMPA<=4;	counta<=4;end
	4:begin numa<=ared;  	LAMPA<=8;	counta<=0;end
	default:						LAMPA<=8;
	endcase
	end
	else 
		begin //倒计时
			if(numa>1)
			if(numa[3:0]==0)
				begin numa[3:0]<=4'b1001;numa[7:4]<=numa[7:4]-1;end
			else numa[3:0]<=numa[3:0]-1;
		if(numa==2) tempa<=0;
		end
	end
	else begin LAMPA<=4'b1000;counta<=0;tempa<=0;end
end
always@(posedge CLK)
begin
	if(EN)
	begin
		if(!tempb)
		begin
			tempb<=1;
			case(countb)
				0:begin numb<=agreen;	LAMPB<=2;	countb<=1;end
				1:begin numb<=ayellow;  LAMPB<=4;	countb<=2;end
				2:begin numb<=aleft;    LAMPB<=1;	countb<=3;end
				3:begin numb<=ayellow;  LAMPB<=4;	countb<=4;end
				4:begin numb<=ared;  	LAMPB<=8;	countb<=0;end
				default:						LAMPB<=8;
			endcase
			